package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Quotation;
import com.ruoyi.system.domain.SalesOrder;
import com.ruoyi.system.mapper.QuotationMapper;
import com.ruoyi.system.mapper.SalesOrderMapper;
import com.ruoyi.system.service.IProgressTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.ProgressTemplateNodeMapper;
import com.ruoyi.system.domain.ProgressTemplateNode;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

@Service
public class ProgressTemplateServiceImpl implements IProgressTemplateService {

    @Autowired
    private ProgressTemplateNodeMapper nodeMapper;

    @Autowired
    private SalesOrderMapper salesOrderMapper;

    @Autowired
    private QuotationMapper quotationMapper;

    @Override
    @Transactional
    public List<ProgressTemplateNode> createProgressFromTemplate(Long templateId, Long orderId, String orderNumber) {
        // 1. 获取模板的所有节点
        List<ProgressTemplateNode> templateNodes = nodeMapper.selectNodesByTemplateId(templateId);
        List<ProgressTemplateNode> newNodes = new ArrayList<>();
        Long firstNodeId = null;  // 用于存储第一个节点的ID

        // 2. 为每个节点创建新的实例，并关联到订单
        for (ProgressTemplateNode templateNode : templateNodes) {
            ProgressTemplateNode newNode = new ProgressTemplateNode();
            newNode.setTemplateId(templateId);
            newNode.setNodeName(templateNode.getNodeName());
            newNode.setNodeOrder(templateNode.getNodeOrder());
            newNode.setOrderNumber(orderNumber);  // 使用订单编号
            newNode.setIsTemplate(false);

            nodeMapper.insertNode(newNode);
            newNodes.add(newNode);

            // 记录第一个节点的ID
            if (templateNode.getNodeOrder() == 1) {
                firstNodeId = newNode.getNodeId();
            }
        }

        // 3. 更新订单的nodeId为第一个节点的ID
        if (firstNodeId != null) {
            if (orderNumber.startsWith("XS")) {
                SalesOrder salesOrder = new SalesOrder();
                salesOrder.setSalesOrderId(orderId);
                salesOrder.setNodeId(firstNodeId);
                salesOrderMapper.updateSalesOrder(salesOrder);
            } else if (orderNumber.startsWith("BJ")) {
                Quotation quotation = new Quotation();
                quotation.setQuotationId(orderId);
                quotation.setNodeId(firstNodeId);
                quotationMapper.updateQuotation(quotation);
            }
        }

        return newNodes;
    }

    @Override
    public Map<String, Object> getOrderProgress(Long orderId, String orderType) {
        Map<String, Object> result = new HashMap<>();
        String orderNumber;
        String orderStatus;

        // 1. 获取订单信息和状态
        if ("销售单".equals(orderType)) {
            SalesOrder order = salesOrderMapper.selectSalesOrderById(orderId);
            orderStatus = order.getStatus();
            orderNumber = order.getNumber();
        } else {
            Quotation order = quotationMapper.getQuotationById(orderId);
            orderStatus = order.getStatus();
            orderNumber = order.getNumber();
        }

        // 2. 获取所有进度节点
        List<ProgressTemplateNode> nodes = nodeMapper.selectNodesByOrderNumber(orderNumber);

        // 3. 找到当前节点(根据订单状态匹配节点名称)
        int currentIndex = 0;
        ProgressTemplateNode currentNode = null;

        for (int i = 0; i < nodes.size(); i++) {
            ProgressTemplateNode node = nodes.get(i);
            if (orderStatus.equals(node.getNodeName())) {
                currentNode = node;
                currentIndex = i;
                break;
            }
        }

        // 4. 设置返回信息
        result.put("nodes", nodes);  // 所有节点
        result.put("currentNode", currentNode);  // 当前节点
        result.put("progress", (currentIndex + 1) * 100 / nodes.size());  // 进度百分比
        result.put("status", orderStatus);  // 订单状态

        return result;
    }
}
